<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
<!--      <el-form-item label="参数名称" prop="configName">
        <el-input
          v-model="queryParams.configName"
          placeholder="请输入参数名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数键名" prop="configKey">
        <el-input
          v-model="queryParams.configKey"
          placeholder="请输入参数键名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统内置" prop="configType">
        <el-select v-model="queryParams.configType" placeholder="系统内置" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>-->
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="订单编号" prop="oid">
            <el-input v-model="queryParams.oid"
                      placeholder="请输入订单编号"
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="订单状态" prop="status">
            <el-select style="width: 240px" v-model="queryParams.status" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="下单时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="timestamp"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="售后" prop="postSaleType">
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="商品名称" prop="itemTitle">
            <el-input v-model="queryParams.itemTitle"
                      placeholder="请输入商品名称"
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="买家ID" prop="buyerMixed">
            <el-input v-model="queryParams.buyerMixed"
                      placeholder="请输入买家ID"
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="快递单号" prop="expressNo">
            <el-input v-model="queryParams.expressNo"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="买家昵称" prop="buyerNick">
            <el-input v-model="queryParams.buyerNick"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="收货人手机号" prop="consigneePhone">
            <el-input v-model="queryParams.consigneePhone"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="收货人姓名" prop="consigneeName">
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <!--   注意名字   -->
          <el-form-item label="商品ID" prop="consigneeName">
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item prop="consigneeName">
            <div slot="label">
              <span>分销者ID</span>
              <el-tooltip content="测试测试" effect="dark" placement="bottom">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </div>
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="活动类型" prop="postSaleType">
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item prop="postSaleType">
            <div slot="label">
              <span>待发货时间</span>
              <el-tooltip content="测试测试" effect="dark" placement="bottom">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </div>
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="承诺发货时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="渠道" prop="postSaleType">
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="催发货" prop="postSaleType">
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="延长自动收货时间" prop="postSaleType">
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="预估佣金" prop="consigneeName">
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 113px"
                      @keyup.enter.native="handleQuery"
            >
              <div slot="prefix">￥</div>
            </el-input>
            <span> - </span>
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 113px"
                      @keyup.enter.native="handleQuery"
            >
              <div slot="prefix">￥</div>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="预估分佣比例" prop="consigneeName">
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 113px"
                      @keyup.enter.native="handleQuery"
            >
              <div slot="prefix">%</div>
            </el-input>
            <span> - </span>
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 113px"
                      @keyup.enter.native="handleQuery"
            >
              <div slot="prefix">%</div>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label="隐私授权状态" prop="postSaleType">
            <el-select style="width: 240px" v-model="queryParams.postSaleType" placeholder="" clearable>
              <el-option
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="8" :xs="24" :sm="12">
          <!--   注意名字   -->
          <el-form-item label="cookie" prop="consigneeName">
            <el-input v-model="queryParams.consigneeName"
                      placeholder=""
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item label=" ">
            <el-checkbox-group v-model="queryParams.type">
              <el-checkbox label="保证金退款" name="type"></el-checkbox>
              <el-checkbox label="退货补运费" name="type"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xs="24" :sm="12">
          <el-form-item style="text-align: center">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleExport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-box"
          size="mini"
          @click="handleExport"
        >合并发货</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-s-promotion"
          size="mini"
          @click="handleExport"
        >打单发货</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs v-model="queryParams.tabName" @tab-click="handleClick">
      <el-tab-pane :label="`全部 ${total}`" name="first"></el-tab-pane>
      <el-tab-pane label="待付款" name="second"></el-tab-pane>
      <el-tab-pane label="待发货" name="third"></el-tab-pane>
      <el-tab-pane label="已发货" name="fourth"></el-tab-pane>
      <el-tab-pane label="已收货" name="five"></el-tab-pane>
      <el-tab-pane label="交易成功" name="six"></el-tab-pane>
      <el-tab-pane label="订单关闭" name="seven"></el-tab-pane>
    </el-tabs>
    <el-table :default-expand-all="true" v-loading="loading" :data="configList" @selection-change="handleSelectionChange" :span-method="spanMethod">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table v-loading="loading" :data="[props.row]" :show-header="false">
            <el-table-column label="商品" align="left" prop="" :show-overflow-tooltip="true" />
            <el-table-column label="渠道" align="center" prop="configName" :show-overflow-tooltip="true" />
            <el-table-column label="售后" align="center" prop="configName" />
            <el-table-column label="订单状态" align="center" prop="configName" />
            <el-table-column label="服务" align="center" prop="configName" :show-overflow-tooltip="true" />
            <el-table-column label="实收款" align="center" prop="configName" :show-overflow-tooltip="true" />
            <el-table-column align="left" prop="" :show-overflow-tooltip="true">
              <template slot="header">
                <span>分佣角色和佣金</span>
                <el-tooltip content="测试测试" effect="dark" placement="bottom">
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >订单详情</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >延长收货时间</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >追加包裹</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>

<!--      <el-table-column label="参数主键" align="center" prop="configId" />-->
      <el-table-column label="商品" align="left" prop="configName" :show-overflow-tooltip="true" />
      <el-table-column label="渠道" align="center" prop="configName" :show-overflow-tooltip="true" />
      <el-table-column label="售后" align="center" prop="configName" />
      <el-table-column label="订单状态" align="center" prop="configName" />
      <el-table-column label="服务" align="center" prop="configName" :show-overflow-tooltip="true" />
      <el-table-column label="实收款" align="center" prop="configName" :show-overflow-tooltip="true" />
      <el-table-column align="left" prop="configName" :show-overflow-tooltip="true">
        <template slot="header">
          <span>分佣角色和佣金</span>
          <el-tooltip content="测试测试" effect="dark" placement="bottom">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      </el-table-column>


    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig, refreshCache } from "@/api/system/config";

export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      form: {},
      // 表单参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configName: undefined,
        configKey: undefined,
        configType: undefined,
        type: [],
        tabName: 'first',
        // 订单编号
        // 订单状态
        // 下单时间（开始）
        // 下单时间（结束）
        // 售后
        // 商品名称
        // 买家ID
        // 快递单号
        // 买家昵称
        // 收货人手机号
        // 收货人姓名
        // 商品ID
        // 分销者ID
        // 活动类型
        // 待发货时间
        // 承诺发货时间
        // 渠道
        // 催发货
        // 延长自动收获时间
        // 预估佣金（开始）
        // 预估佣金（结束）
        // 预估分佣比例（开始）
        // 预估分佣比例（结束）
        // 隐私授权状态

      },
      // 表单校验
      rules: {
        configName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
        configKey: [
          { required: true, message: "参数键名不能为空", trigger: "blur" }
        ],
        configValue: [
          { required: true, message: "参数键值不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.configList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: undefined,
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: "Y",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids
      getConfig(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != undefined) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(function() {
          return delConfig(configIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    },
    handleClick(tab, event) {
      //console.log(tab, event);
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        return {
          rowspan: 1,
          colspan: 1
        }
      } else if (columnIndex === 1) {
        return {
          rowspan: 1,
          colspan: 6
        }
      } else if (columnIndex === 7) {
        return {
          rowspan: 1,
          colspan: 2
        }
      } else {
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    },
  }
};
</script>
